/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.jonasjelten.jcalc.mathExps;

import de.jonasjelten.jcalc.exception.JEvaluationException;
import de.jonasjelten.jcalc.math.JMathOps;
import de.jonasjelten.jcalc.math.JNumber;
import java.math.BigDecimal;

/**
 *
 * @author JJ
 */
public class JPrimeTest extends JSingleArgMathExp {

    public JPrimeTest(JMathExp arg) {
        super(arg);
    }

    @Override
    public JNumber eval() throws JEvaluationException {
        JNumber res = super.eval();
        if(!res.isNonNegativeRealInteger()) {
            throw new JEvaluationException("Primality can only be checked for a non negative real integer.");
        }
        super.jcalc.expgroup.addActive();
        res = JMathOps.isPrime(res.realNumerator) ? new JNumber(1) : new JNumber(0);
        super.jcalc.expgroup.setInactive();
        return res;
    }

    @Override
    public boolean isTrivial() {
        return false;
    }

    @Override
    public String toString() {
        return "isPrime("+super.arg.toString()+")";
    }

}
